In [1]:
%pylab inline

from matplotlib import animation
Populating the interactive namespace from numpy and matplotlib
In [2]:
from IPython.display import HTML
In [3]:
def wave(x,t,k0,c,sigmak,pkmax,nstep):
    total=0
    dk=2*pkmax*sigmak/nstep
    for i in range(nstep):
        k=k0 - pkmax *sigmak+i*dk
        omega=c*k + 1 * (k-k0)*(k-k0)
        total += exp(-(k-k0)**2/sigmak)*exp(1j*(k*x-omega*t))*dk
        # print (i,'k=',k)
    return (total.real)
In [4]:
#     k0,c,sigmak,pkmax,nstep
params=[1, .2, 0.1, 5, 20] 
In [5]:
wave(2.7, 10, *params)
Out[5]:
0.45192774928980284
In [6]:
k0=1
sigmak=0.02*k0
c=0.1
pkmax=5
kstep=10

xx=10
xmin=-xx*2*pi/k0
xmax=xx*2*pi/k0
xstep=300

nstep=20

params=[k0,c,sigmak,pkmax,nstep]
ymax=wave(0, 0, k0, c,sigmak, pkmax, kstep)
In [7]:
x = np.linspace(xmin, xmax, xstep)
y=wave(x, 0, k0, c,sigmak, pkmax, kstep)

print('ymax =',ymax)
plot(x,y);
ymax = 0.17071998392138688
In [8]:
%%capture
# create a simple animation
fig = plt.figure()
ax = plt.axes(xlim=(xmin,xmax), ylim=(-ymax, ymax))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    line.set_data(x, wave(x, i,*params))
    return line,
In [9]:
HTML(animation.FuncAnimation(fig, animate, init_func=init,
                        frames=linspace(-300,700,60),interval=20, blit=True).to_jshtml())
Out[9]: